package com.digital.herbarium.Controller;

import com.digital.herbarium.annotation.CheckToken;
import com.digital.herbarium.Entity.Image;
import com.digital.herbarium.Entity.ResponseResult;
import com.digital.herbarium.annotation.Log;
import com.digital.herbarium.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/image")
public class PlantImageController {

    @Autowired
    private ImageService imageService;
    @Value("${static.excelurl}")
    private String excelurl;


    @GetMapping("getImageInfo")
    @Log("plantImage")
    public ResponseResult getImageInfo(Image image,
                                       @RequestParam(value = "pageSize",defaultValue = "10",required = false) int pageSize,
                                       @RequestParam(value = "pageNum",defaultValue = "0",required = false) int pageNum) {
        return ResponseResult.SUCCESS().setData(imageService.getImageInfo(image, pageSize, pageNum));
    }

    @GetMapping("getManagerImageInfo")
    @CheckToken()
    @Log("plantImage")
    public ResponseResult getManagerImageInfo(Image image,
                                       @RequestParam(value = "pageSize",defaultValue = "10",required = false) int pageSize,
                                       @RequestParam(value = "pageNum",defaultValue = "0",required = false) int pageNum) {
        return ResponseResult.SUCCESS().setData(imageService.getImageInfo(image, pageSize, pageNum));
    }


    @PostMapping("addImageInfo")
    @CheckToken()
    public  ResponseResult addImageInfo(Image image){
        imageService.addImageInfo(image);
        return ResponseResult.SUCCESS().setData("添加成功");
    }

    @PostMapping("editImageInfo")
    @CheckToken()
    public  ResponseResult editImageInfo(Image image){
        imageService.editImageInfo(image);
        return ResponseResult.SUCCESS().setData("更新成功");
    }

    @GetMapping("delImageInfo")
    @CheckToken()
    public ResponseResult delImageInfo(String id){
        imageService.delImageInfo(id);
        return  ResponseResult.SUCCESS().setData("删除成功");
    }


    @PostMapping("uploadExcel")
    @CheckToken()
    public  ResponseResult uploadExcel(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request){
        imageService.addExcel(file,request,excelurl);
        return ResponseResult.SUCCESS().setData("上传成功");
    }

}
